package com.xyy.saas.payment.cores.enums;

import java.util.Objects;

public enum TransactionStatusEnum {
    TRANSACTING("0","处理中"),
    SUCCESS("1","成功"),
    FAIL("2","失败"),
    NOT_FOUNT("3","无此交易"),
    TRADE_CLOSE("5","交易关闭"),
    REFUNDED("15","退汇"),
    HANGING_ORDER("98","挂单"),
    CANCEL("99","取消")
    ;

    private String code;
    private String desc;

    TransactionStatusEnum(String code,String desc) {
        this.code = code;
        this.desc = desc;
    }

    /**
     * 获取枚举
     * @param code
     * @return
     */
    public static TransactionStatusEnum code(Integer code) {
        for(TransactionStatusEnum item : values()) {
            if(Objects.equals(Integer.valueOf(item.code), code)) {
                return item;
            }
        }

        return null;
    }

    /**
     * 获取枚举
     * @param code
     * @return
     */
    public static TransactionStatusEnum code(String code) {
        for(TransactionStatusEnum item : values()) {
            if(Objects.equals(item.code, code)) {
                return item;
            }
        }

        return null;
    }

    public static TransactionStatusEnum convertAbchinaLoan(AbchinaLoanOrderStatusEnum abchinaLoanOrderStatusEnum) {
        if (abchinaLoanOrderStatusEnum == null) {
            return null;
        }
        switch (abchinaLoanOrderStatusEnum) {
            case PAID:
                return SUCCESS;
            case UNPAID:
                return TRANSACTING;
        }
        return null;
    }


    /**
     * 平安支付状态 -> 支付平台交易状态
     */
    public static TransactionStatusEnum convertPingPayStatus(String status) {
        if(status == null) {
            return NOT_FOUNT;
        }

        switch(status) {
            case "0":
            case "2":
            case "3":
                return TRANSACTING;
            case "1":
                return SUCCESS;
        }

        return NOT_FOUNT;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }
}
